import { inject, observer } from 'mobx-react';
import { WeaTools, WeaProgress, WeaDialog } from 'ecCom';
import BaseList from '../BaseList';
import * as Rctt from '../../constants/BaseListConstant';
// import WeaCrm from 'weaCrm';
import { Button } from 'antd';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

@inject('customerReportStore')
@observer
class CustomerReport extends BaseList {
  getStore() {
    // console.log(WeaCrm);
    return this.props.customerReportStore;
  }

  getTabSearchTypes(store) {
    const arr = [];
    arr.push(Rctt.TAB_SEARCH_TYPE_BASE);
    return arr;
  }

  reRenderColumns(columns) {
    columns.forEach((c) => {
      if (c.dataIndex == 'resultcount') {
        c.render = function (text, record) {
          if (!record.hasOwnProperty('_stat')) {
            return <span className="wea-crmReport-progressStyle"><WeaProgress ecId={`${this && this.props && this.props.ecId || ''}_WeaProgress@l5fmb5`} percent={record.resultcountspan} /></span>;
          }
          return <div />;
        };
      }
      if (c.dataIndex == 'resultcount_n') {
        const that = this;
        const store = this.getStore();
        const { type } = store.routerParams;
        c.render = function (text, record) {
          let keyWord = '';
          let keyWordValue = '';
          if (type == 'typeList') {
            keyWord = 'type';
          } else if (type == 'statusList') {
            keyWord = 'status';
          } else if (type == 'descList') {
            keyWord = 'description';
          } else if (type == 'managerList') {
            keyWord = 'manager';
          } else if (type == 'deptList') {
            keyWord = 'department';
          } else if (type == 'contList') {
            keyWord = 'source';
          } else if (type == 'sectorList') {
            keyWord = 'sector';
          } else if (type == 'sizeList') {
            keyWord = 'size_n';
          }
          keyWordValue = record.randomFieldId;
          let valueSpan;
          if (type == 'deptList') {
            valueSpan = record.departmentid;
          } else {
            valueSpan = record[keyWord];
          }
          if (!record.hasOwnProperty('_stat')) {
            const url = `/spa/crm/static/index.html#/main/crm/customer/report?${keyWord}=${keyWordValue}`;
            return <span onClick={() => { store.showCustomersDialog(url); }} ><a>{record[c.dataIndex]}</a></span>;
          }
          return <span>{record.resultcount_n}</span>;
        };
      }
    });
    return columns;
  }

  renderAppend(store) {
    const { dialog, dialogChangeState } = store;
    return [<WeaDialog
      ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@egx6ps@dialog`}
      icon="icon-coms-crm"
      iconBgcolor="#96358a"
      title={dialog.title}
      visible={dialog.visible}
      url={dialog.url}
      onCancel={store.hideCustomersDialog}
      style={dialog.style}
      buttons={[<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@zpqzbo@close`} type="primary" onClick={store.hideCustomersDialog}>{getLabel(309, '关闭')}</Button>]}
    />];
  }
}

export default WeaTools.tryCatch(React, props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@2yf9v1`} msg={props.error ? props.error : getLabel(129417, '服务器运行出错，请联系系统管理员！')} />, { error: '' })(CustomerReport);
